package ntd.util;

import com.c.rabbit_mq.impl.Iot212MsgExe;
import com.c.rabbit_mq.impl.IotCmdExe;
import com.c.rabbit_mq.impl.IotMsgPush;
import ntd.Init;
import ntd.remote.link.ComRegistryServer;
import ntd.util.frame.task.handle.ServerManagerService;
import ntd.util.frame.task.handle.TaskManagerService;
import ntd.util.frame.task.handle.iot.TaskIotCmdExe;
import ntd.util.frame.websocket.WebSocketWithCmd;
import ntd.util.modular.data.service.DataAcquisitionRuleService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Order(9)
public class InitAfterRunnerImpl implements CommandLineRunner {

    private ServerManagerService serverManagerService = new ServerManagerService();

    @Resource
    private DataAcquisitionRuleService dataAcquisitionRuleService;
    /**
     * springboot 加载后执行
     * @return
     */
    @Override
    public void run(String... args) {
        // 初始化附加服务
        UtilCatch.tryCatchNoRes(() -> {
            UtilThreadPool.run(Init::init);

            // 执行任务同步
            if (ComConfig.com.getBool("com.task.open")) {
                UtilThreadPool.periodRun(TaskManagerService::executeTask, 180);
            }

            // 执行服务同步
            if (ComConfig.com.getBool("com.server.listen")) {
                UtilThreadPool.periodRun(() -> serverManagerService.execute(null), 180);

                UtilThreadPool.periodRun(() -> new TaskIotCmdExe().execute(null), 5);

                // 注册Cmd指令执行函数
//                new IotCmdExe().start_rabbit_mq_listen();
                // 注册回推消息
//                new IotMsgPush().start_rabbit_mq_listen();
            }

            if (ComConfig.com.getBool("com.server.msg")) {
                // 212消息执行监听
                UtilThreadPool.run(() -> new Iot212MsgExe().start_rabbit_mq_listen());
            }

            // webssh 初始化
            UtilThreadPool.run(WebSocketWithCmd::init);

            // 规则运行
//            dataAcquisitionRuleService.initRule();

            // 注册中心功能 - 待完善
//            LinkClient.run("公共服务中心", "115.29.192.252");
        });
    }
}
